package com.wx.wxsecondskill.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import com.wx.redis.RedisUtils;
import com.wx.wxsecondskill.po.User;
import com.wx.wxsecondskill.service.GoodsService;
import com.wx.wxsecondskill.service.UserService;
import com.wx.wxsecondskill.vo.GoodsDetailVo;
import com.wx.wxsecondskill.vo.GoodsVo;

/**
 * @ClassName GoodsController
 * @Author wx
 * @Description 商品Controller
 * @Date 2018-11-15-21:01
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
	/** 商品列表缓存key */
	private final static String GOODSLISTKEY = "goods_list";

	/** 商品列表缓存key过期时间 60秒 */
	private final static Integer GOODSLISTKEYEXPIRE = 60;

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private UserService userService;

	@Autowired
	private ThymeleafViewResolver thymeleafViewResolver;

	/**
	 * @methodName: list
	 * @author: wx
	 * @description: 原始访问商品列表 3000/s并发 QPS=1033/s
	 * @param
	 * @date: 2018/11/17
	 * @return: org.springframework.web.servlet.ModelAndView
	 */
	@GetMapping(value = "/to_list")
	public ModelAndView list() {
		ModelAndView modelAndView = new ModelAndView("goods_list");
		List<GoodsVo> goodsList = goodsService.listGoodsVo();
		modelAndView.addObject("goodsList", goodsList);
		return modelAndView;
	}

	/**
	 * @methodName: listV2
	 * @author: wx
	 * @description: 新增页面缓存的访问商品列表 3000/s并发 QPS=1976/s
	 * @param
	 * @date: 2018/11/17
	 * @return: org.springframework.web.servlet.ModelAndView
	 */
	@ResponseBody
	@GetMapping(value = "/to_listV2", produces = "text/html;charset=UTF-8")
	public String listV2(HttpServletRequest request, HttpServletResponse response) {
		// 获取redis存储的页面缓存
		String goodsListHtml = (String) RedisUtils.redisClient.get(GOODSLISTKEY);
		if (StringUtils.isNotBlank(goodsListHtml)) {
			// 如果存在就直接返回html内容
			return goodsListHtml;
		}
		// 不存在去数据库查询，在用thymeleafViewResolver根据模板手动渲染出html内容存储进redis并html内容
		List<GoodsVo> goodsList = goodsService.listGoodsVo();
		Map<String, Object> goodsData = new HashMap<>(1);
		goodsData.put("goodsList", goodsList);
		WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale(), goodsData);
		// 手动渲染
		goodsListHtml = thymeleafViewResolver.getTemplateEngine().process("goods_list", ctx);
		RedisUtils.redisClient.set(GOODSLISTKEY, goodsListHtml, GOODSLISTKEYEXPIRE);
		return goodsListHtml;
	}

	@ResponseBody
	@GetMapping(value = "/to_detail/{goodsId}")
	public ModelAndView detail(@PathVariable("goodsId") Integer goodsId) {
		ModelAndView modelAndView = new ModelAndView("goods_detail");
		GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
		User user = userService.getFirstUser();
		if (user == null) {
			modelAndView.addObject("errmsg", "用户不存在");
			modelAndView.setViewName("miaosha_fail");
			return modelAndView;
		}
		GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
		goodsDetailVo.setGoods(goods);
		goodsDetailVo.setUser(user);
		modelAndView.addObject("goodsDetail", goodsDetailVo);
		return modelAndView;
	}

}
